package com.example.ccj.b.c.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.ccj.R;
import com.example.ccj.u.SizeUtils;

/**
 * 图片加文字按钮，左图片右按钮
 * 可设置点击间隔以及是否只能点击一次
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/28 13:53
 */
public class ImageAndTextButton extends LinearLayout implements View.OnClickListener, ViewTreeObserver.OnGlobalLayoutListener{
    ImageView img;

    TextView texView;

    String text = "";       //文本
    float textSize = 10;      //文本大小
    @ColorInt int textColor;//文本颜色
    @DrawableRes int src;   //图片资源
    boolean onlyClickOne = false;//是否只能点击一次
    int clickInterval = 300;//每次点击间隔
    long clickTime = 0;     //上次点击的时间戳



    public ImageAndTextButton(Context context) {
        super(context);
        initView();
    }

    public ImageAndTextButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageAndTextButton);
        text = typedArray.getString(R.styleable.ImageAndTextButton_text);
        textSize = typedArray.getDimension(R.styleable.ImageAndTextButton_text_size,textSize);
        textColor = typedArray.getColor(R.styleable.ImageAndTextButton_text_color, Color.parseColor("#DDDDDD"));
        src = typedArray.getResourceId(R.styleable.ImageAndTextButton_src,R.mipmap.def_img_select);
        clickInterval = typedArray.getInteger(R.styleable.ImageAndTextButton_click_interval,clickInterval);
        onlyClickOne = typedArray.getBoolean(R.styleable.ImageAndTextButton_click_only_one,false);


        initView();

        texView.setText(text);
        texView.setTextSize(SizeUtils.px2sp(textSize));
        texView.setTextColor(textColor);
        img.setImageResource(src);

        //添加布局发生变化时监听，这里使用它来确保在测量完成后调用绘制图片高度
        img.getViewTreeObserver().addOnGlobalLayoutListener(this);

        typedArray.recycle();
    }

    /**
     * 设置图片的宽度和高度一致
     * 要给图片添加以下监听才会调用下面这段代码
     * img.getViewTreeObserver().addOnGlobalLayoutListener(this);
     */
    @Override
    public void onGlobalLayout() {
        int height = img.getMeasuredHeight();
        if (height > 0) {
            ViewGroup.LayoutParams layoutParams = img.getLayoutParams();
            layoutParams.width = height;
            img.setLayoutParams(layoutParams);
            img.invalidate();
            img.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    }


    private void initView(){
        inflate(getContext(), R.layout.custom_image_and_text_button,this);
        texView = findViewById(R.id.custom_image_and_text_button_text);
        img = findViewById(R.id.custom_image_and_text_button_img);
        super.setOnClickListener(this);
    }




    public ImageAndTextButton setText(@NonNull String text) {
        this.text = text;
        texView.post(()->this.texView.setText(text));
        return this;
    }
    public ImageAndTextButton setTextSize(float textSize) {
        this.textSize = textSize;
        texView.post(()->this.texView.setTextSize(textSize));
        return this;
    }
    public ImageAndTextButton setTextColor(@ColorInt int textColor) {
        this.textColor = textColor;
        texView.post(()->this.texView.setTextColor(textColor));
        return this;
    }
    public ImageAndTextButton setSrc(@DrawableRes int src) {
        this.src = src;
        img.post(()-> this.img.setImageResource(src));
        return this;
    }
    public ImageAndTextButton setOnlyClickOne(boolean onlyClickOne) {
        this.onlyClickOne = onlyClickOne;
        return this;
    }
    public ImageAndTextButton setClickInterval(int clickInterval) {
        this.clickInterval = clickInterval;
        return this;
    }
    public ImageAndTextButton setClickTime(long clickTime) {
        this.clickTime = clickTime;
        return this;
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        this.onClickListenerN = l;
    }


    public String getText() {
        return text;
    }

    public float getTextSize() {
        return textSize;
    }

    public int getTextColor() {
        return textColor;
    }

    public int getSrc() {
        return src;
    }

    public boolean isOnlyClickOne() {
        return onlyClickOne;
    }

    public int getClickInterval() {
        return clickInterval;
    }

    public long getClickTime() {
        return clickTime;
    }


    public ImageView getImg() {
        return img;
    }

    public TextView getTexView() {
        return texView;
    }

    public OnClickListener getOnClickListenerN() {
        return onClickListenerN;
    }

    OnClickListener onClickListenerN;

    @Override
    public void onClick(View v) {
        if (onlyClickOne) {
            if (onClickListenerN != null) onClickListenerN.onClick(v);
            this.setEnabled(false);
        } else if (!(System.currentTimeMillis() - clickTime < clickInterval)) {
            clickTime = System.currentTimeMillis();
            if (onClickListenerN != null) onClickListenerN.onClick(v);
        }
    }



}
